home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / src / ctime.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  36KB  |  1,337 lines

  1. /*
  2.  * Copyright (c) 1987, 1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Arthur David Olson of the National Cancer Institute.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *      This product includes software developed by the University of
  19.  *      California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)ctime.c    5.26 (Berkeley) 2/23/91";
  39. #endif                /* LIBC_SCCS and not lint */
  40.  
  41. /*
  42.    ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
  43.    ** POSIX-style TZ environment variable handling from Guy Harris
  44.    ** (guy@auspex.com).
  45.  */
  46.  
  47. /*LINTLIBRARY */
  48.  
  49. #include <sys/param.h>
  50. #include <fcntl.h>
  51. #include <time.h>
  52. #include <tzfile.h>
  53. #include <string.h>
  54. #include <ctype.h>
  55. #include <stdio.h>
  56. #include <unistd.h>
  57.  
  58. #ifdef __STDC__
  59. #include <stdlib.h>
  60.  
  61. #define P(s)        s
  62. #define alloc_size_t    size_t
  63. #define qsort_size_t    size_t
  64. #define fread_size_t    size_t
  65. #define fwrite_size_t    size_t
  66.  
  67. #else                /* !defined __STDC__ */
  68.  
  69. #define P(s)        ()
  70.  
  71. typedef char *genericptr_t;
  72. typedef unsigned alloc_size_t;
  73. typedef int qsort_size_t;
  74. typedef int fread_size_t;
  75. typedef int fwrite_size_t;
  76.  
  77. extern char *calloc();
  78. extern char *malloc();
  79. extern char *realloc();
  80. extern char *getenv();
  81.  
  82. #endif                /* !defined __STDC__ */
  83.  
  84. extern time_t time();
  85.  
  86. #define ACCESS_MODE    O_RDONLY
  87. #define OPEN_MODE    O_RDONLY
  88.  
  89. #ifndef WILDABBR
  90. /*
  91.    ** Someone might make incorrect use of a time zone abbreviation:
  92.    **   1.      They might reference tzname[0] before calling tzset (explicitly
  93.    **           or implicitly).
  94.    **   2.      They might reference tzname[1] before calling tzset (explicitly
  95.    **           or implicitly).
  96.    **   3.      They might reference tzname[1] after setting to a time zone
  97.    **           in which Daylight Saving Time is never observed.
  98.    **   4.      They might reference tzname[0] after setting to a time zone
  99.    **           in which Standard Time is never observed.
  100.    **   5.      They might reference tm.TM_ZONE after calling offtime.
  101.    ** What's best to do in the above cases is open to debate;
  102.    ** for now, we just set things up so that in any of the five cases
  103.    ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
  104.    ** string "tzname[0] used before set", and similarly for the other cases.
  105.    ** And another:  initialize tzname[0] to "ERA", with an explanation in the
  106.    ** manual page of what this "time zone abbreviation" means (doing this so
  107.    ** that tzname[0] has the "normal" length of three characters).
  108.  */
  109. #define WILDABBR    "   "
  110. #endif                /* !defined WILDABBR */
  111.  
  112. #ifndef TRUE
  113. #define TRUE        1
  114. #define FALSE        0
  115. #endif                /* !defined TRUE */
  116.  
  117. static const char GMT[] = "GMT";
  118.  
  119. struct ttinfo {            /* time type information */
  120.     long tt_gmtoff;        /* GMT offset in seconds */
  121.     int tt_isdst;        /* used to set tm_isdst */
  122.     int tt_abbrind;        /* abbreviation list index */
  123.     int tt_ttisstd;        /* TRUE if transition is std time */
  124. };
  125.  
  126. struct lsinfo {            /* leap second information */
  127.     time_t ls_trans;        /* transition time */
  128.     long ls_corr;        /* correction to apply */
  129. };
  130.  
  131. struct state {
  132.     int leapcnt;
  133.     int timecnt;
  134.     int typecnt;
  135.     int charcnt;
  136.     time_t ats[TZ_MAX_TIMES];
  137.     unsigned char types[TZ_MAX_TIMES];
  138.     struct ttinfo ttis[TZ_MAX_TYPES];
  139.     char chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ?
  140.            TZ_MAX_CHARS + 1 : sizeof GMT];
  141.     struct lsinfo lsis[TZ_MAX_LEAPS];
  142. };
  143.  
  144. struct rule {
  145.     int r_type;            /* type of rule--see below */
  146.     int r_day;            /* day number of rule */
  147.     int r_week;            /* week number of rule */
  148.     int r_mon;            /* month number of rule */
  149.     long r_time;        /* transition time of rule */
  150. };
  151.  
  152. #define    JULIAN_DAY        0    /* Jn - Julian day */
  153. #define    DAY_OF_YEAR        1    /* n - day of year */
  154. #define    MONTH_NTH_DAY_OF_WEEK    2    /* Mm.n.d - month, week, day of week */
  155.  
  156. /*
  157.    ** Prototypes for static functions.
  158.  */
  159.  
  160. static long detzcode P((const char *codep));
  161. static const char *getzname P((const char *strp));
  162. static const char *getnum P((const char *strp, int *nump, int min,
  163.                  int max));
  164. static const char *getsecs P((const char *strp, long *secsp));
  165. static const char *getoffset P((const char *strp, long *offsetp));
  166. static const char *getrule P((const char *strp, struct rule * rulep));
  167. static void gmtload P((struct state * sp));
  168. static void gmtsub P((const time_t * timep, long offset,
  169.               struct tm * tmp));
  170. static void localsub P((const time_t * timep, long offset,
  171.             struct tm * tmp));
  172. static void normalize P((int *tensptr, int *unitsptr, int base));
  173. static void settzname P((void));
  174. static time_t time1 P((struct tm * tmp, void (*funcp) (),
  175.                long offset));
  176. static time_t time2 P((struct tm * tmp, void (*funcp) (),
  177.                long offset, int *okayp));
  178. static void timesub P((const time_t * timep, long offset,
  179.                const struct state * sp, struct tm * tmp));
  180. static int tmcomp P((const struct tm * atmp,
  181.              const struct tm * btmp));
  182. static time_t transtime P((time_t janfirst, int year,
  183.                const struct rule * rulep, long offset));
  184. static int tzload P((const char *name, struct state * sp));
  185. static int tzparse P((const char *name, struct state * sp,
  186.               int lastditch));
  187.  
  188. #ifdef ALL_STATE
  189. static struct state *lclptr;
  190. static struct state *gmtptr;
  191. #endif                /* defined ALL_STATE */
  192.  
  193. #ifndef ALL_STATE
  194. static struct state lclmem;
  195. static struct state gmtmem;
  196. #define lclptr        (&lclmem)
  197. #define gmtptr        (&gmtmem)
  198. #endif                /* State Farm */
  199.  
  200. static int lcl_is_set;
  201. static int gmt_is_set;
  202.  
  203. char *tzname[2] =
  204. {
  205.     WILDABBR,
  206.     WILDABBR
  207. };
  208.  
  209. time_t timezone = 0;
  210. int daylight = 0;
  211.  
  212. #ifdef ALTZONE
  213. time_t altzone = 0;
  214. #endif                /* defined ALTZONE */
  215.  
  216. static long detzcode(const char *codep)
  217. {
  218.     register long result;
  219.     register int i;
  220.  
  221.     result = 0;
  222.     for (i = 0; i < 4; ++i)
  223.     result = (result << 8) | (codep[i] & 0xff);
  224.     return result;
  225. }
  226.  
  227. static void settzname(void)
  228. {
  229.     register const struct state *sp = lclptr;
  230.     register int i;
  231.  
  232.     tzname[0] = WILDABBR;
  233.     tzname[1] = WILDABBR;
  234.     daylight = 0;
  235.     timezone = 0;
  236. #ifdef ALTZONE
  237.     altzone = 0;
  238. #endif                /* defined ALTZONE */
  239. #ifdef ALL_STATE
  240.     if (sp == NULL) {
  241.     tzname[0] = tzname[1] = GMT;
  242.     return;
  243.     }
  244. #endif                /* defined ALL_STATE */
  245.     for (i = 0; i < sp->typecnt; ++i) {
  246.     register const struct ttinfo *ttisp = &sp->ttis[i];
  247.  
  248.     tzname[ttisp->tt_isdst] =
  249.         (char *) &sp->chars[ttisp->tt_abbrind];
  250.     if (ttisp->tt_isdst)
  251.         daylight = 1;
  252.     if (i == 0 || !ttisp->tt_isdst)
  253.         timezone = -(ttisp->tt_gmtoff);
  254. #ifdef ALTZONE
  255.     if (i == 0 || ttisp->tt_isdst)
  256.         altzone = -(ttisp->tt_gmtoff);
  257. #endif                /* defined ALTZONE */
  258.     }
  259.     /*
  260.        ** And to get the latest zone names into tzname. . .
  261.      */
  262.     for (i = 0; i < sp->timecnt; ++i) {
  263.     register const struct ttinfo *ttisp =
  264.     &sp->ttis[sp->types[i]];
  265.  
  266.     tzname[ttisp->tt_isdst] =
  267.         (char *) &sp->chars[ttisp->tt_abbrind];
  268.     }
  269. }
  270.  
  271. static int tzload(register const char *name, register struct state *sp)
  272. {
  273.     register const char *p;
  274.     register int i;
  275.     register int fid;
  276.  
  277.     if (name == NULL && (name = TZDEFAULT) == NULL)
  278.     return -1;
  279.     {
  280.     char fullname[FILENAME_MAX + 1];
  281.  
  282.     if (name[0] == ':')
  283.         ++name;
  284.     if (!strchr(name, ':')) {
  285.         if ((p = TZDIR) == NULL)
  286.         return -1;
  287.         if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
  288.         return -1;
  289.         (void) strcpy(fullname, p);
  290.         (void) strcat(fullname, "/");
  291.         (void) strcat(fullname, name);
  292.         name = fullname;
  293.     }
  294.     if ((fid = open(name, OPEN_MODE)) == -1)
  295.         return -1;
  296.     }
  297.     {
  298.     register const struct tzhead *tzhp;
  299.     char buf[sizeof *sp + sizeof *tzhp];
  300.     int ttisstdcnt;
  301.  
  302.     i = read(fid, buf, sizeof buf);
  303.     if (close(fid) != 0 || i < sizeof *tzhp)
  304.         return -1;
  305.     tzhp = (struct tzhead *) buf;
  306.     ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt);
  307.     sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt);
  308.     sp->timecnt = (int) detzcode(tzhp->tzh_timecnt);
  309.     sp->typecnt = (int) detzcode(tzhp->tzh_typecnt);
  310.     sp->charcnt = (int) detzcode(tzhp->tzh_charcnt);
  311.     if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
  312.         sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
  313.         sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
  314.         sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
  315.         (ttisstdcnt != sp->typecnt && ttisstdcnt != 0))
  316.         return -1;
  317.     if (i < sizeof *tzhp +
  318.         sp->timecnt * (4 + sizeof(char)) +
  319.         sp->typecnt * (4 + 2 * sizeof(char)) +
  320.         sp->charcnt * sizeof(char) +
  321.         sp->leapcnt * 2 * 4 +
  322.         ttisstdcnt * sizeof(char))
  323.          return -1;
  324.     p = buf + sizeof *tzhp;
  325.     for (i = 0; i < sp->timecnt; ++i) {
  326.         sp->ats[i] = detzcode(p);
  327.         p += 4;
  328.     }
  329.     for (i = 0; i < sp->timecnt; ++i) {
  330.         sp->types[i] = (unsigned char) *p++;
  331.         if (sp->types[i] >= sp->typecnt)
  332.         return -1;
  333.     }
  334.     for (i = 0; i < sp->typecnt; ++i) {
  335.         register struct ttinfo *ttisp;
  336.  
  337.         ttisp = &sp->ttis[i];
  338.         ttisp->tt_gmtoff = detzcode(p);
  339.         p += 4;
  340.         ttisp->tt_isdst = (unsigned char) *p++;
  341.         if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
  342.         return -1;
  343.         ttisp->tt_abbrind = (unsigned char) *p++;
  344.         if (ttisp->tt_abbrind < 0 ||
  345.         ttisp->tt_abbrind > sp->charcnt)
  346.         return -1;
  347.     }
  348.     for (i = 0; i < sp->charcnt; ++i)
  349.         sp->chars[i] = *p++;
  350.     sp->chars[i] = '\0';    /* ensure '\0' at end */
  351.     for (i = 0; i < sp->leapcnt; ++i) {
  352.         register struct lsinfo *lsisp;
  353.  
  354.         lsisp = &sp->lsis[i];
  355.         lsisp->ls_trans = detzcode(p);
  356.         p += 4;
  357.         lsisp->ls_corr = detzcode(p);
  358.         p += 4;
  359.     }
  360.     for (i = 0; i < sp->typecnt; ++i) {
  361.         register struct ttinfo *ttisp;
  362.  
  363.         ttisp = &sp->ttis[i];
  364.         if (ttisstdcnt == 0)
  365.         ttisp->tt_ttisstd = FALSE;
  366.         else {
  367.         ttisp->tt_ttisstd = *p++;
  368.         if (ttisp->tt_ttisstd != TRUE &&
  369.             ttisp->tt_ttisstd != FALSE)
  370.             return -1;
  371.         }
  372.     }
  373.     }
  374.     return 0;
  375. }
  376.  
  377. static const int mon_lengths[2][MONSPERYEAR] =
  378. {
  379.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  380.     31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  381. };
  382.  
  383. static const int year_lengths[2] =
  384. {
  385.     DAYSPERNYEAR, DAYSPERLYEAR
  386. };
  387.  
  388. /*
  389.    ** Given a pointer into a time zone string, scan until a character that is not
  390.    ** a valid character in a zone name is found.  Return a pointer to that
  391.    ** character.
  392.  */
  393.  
  394. static const char *
  395.  getzname(register const char *strp)
  396. {
  397.     register char c;
  398.  
  399.     while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
  400.        c != '+')
  401.     ++strp;
  402.     return strp;
  403. }
  404.  
  405. /*
  406.    ** Given a pointer into a time zone string, extract a number from that string.
  407.    ** Check that the number is within a specified range; if it is not, return
  408.    ** NULL.
  409.    ** Otherwise, return a pointer to the first character not part of the number.
  410.  */
  411.  
  412. static const char *
  413.  getnum(const char *strp, int *nump, int min, int max)
  414. {
  415.     register char c;
  416.     register int num;
  417.  
  418.     if (strp == NULL || !isdigit(*strp))
  419.     return NULL;
  420.     num = 0;
  421.     while ((c = *strp) != '\0' && isdigit(c)) {
  422.     num = num * 10 + (c - '0');
  423.     if (num > max)
  424.         return NULL;    /* illegal value */
  425.     ++strp;
  426.     }
  427.     if (num < min)
  428.     return NULL;        /* illegal value */
  429.     *nump = num;
  430.     return strp;
  431. }
  432.  
  433. /*
  434.    ** Given a pointer into a time zone string, extract a number of seconds,
  435.    ** in hh[:mm[:ss]] form, from the string.
  436.    ** If any error occurs, return NULL.
  437.    ** Otherwise, return a pointer to the first character not part of the number
  438.    ** of seconds.
  439.  */
  440.  
  441. static const char *
  442.  getsecs(register const char *strp, long *secsp)
  443. {
  444.     int num;
  445.  
  446.     strp = getnum(strp, &num, 0, HOURSPERDAY);
  447.     if (strp == NULL)
  448.     return NULL;
  449.     *secsp = num * SECSPERHOUR;
  450.     if (*strp == ':') {
  451.     ++strp;
  452.     strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
  453.     if (strp == NULL)
  454.         return NULL;
  455.     *secsp += num * SECSPERMIN;
  456.     if (*strp == ':') {
  457.         ++strp;
  458.         strp = getnum(strp, &num, 0, SECSPERMIN - 1);
  459.         if (strp == NULL)
  460.         return NULL;
  461.         *secsp += num;
  462.     }
  463.     }
  464.     return strp;
  465. }
  466.  
  467. /*
  468.    ** Given a pointer into a time zone string, extract an offset, in
  469.    ** [+-]hh[:mm[:ss]] form, from the string.
  470.    ** If any error occurs, return NULL.
  471.    ** Otherwise, return a pointer to the first character not part of the time.
  472.  */
  473.  
  474. static const char *
  475.  getoffset(register const char *strp, long *offsetp)
  476. {
  477.     register int neg;
  478.  
  479.     if (*strp == '-') {
  480.     neg = 1;
  481.     ++strp;
  482.     } else if (isdigit(*strp) || *strp++ == '+')
  483.     neg = 0;
  484.     else
  485.     return NULL;        /* illegal offset */
  486.     strp = getsecs(strp, offsetp);
  487.     if (strp == NULL)
  488.     return NULL;        /* illegal time */
  489.     if (neg)
  490.     *offsetp = -*offsetp;
  491.     return strp;
  492. }
  493.  
  494. /*
  495.    ** Given a pointer into a time zone string, extract a rule in the form
  496.    ** date[/time].  See POSIX section 8 for the format of "date" and "time".
  497.    ** If a valid rule is not found, return NULL.
  498.    ** Otherwise, return a pointer to the first character not part of the rule.
  499.  */
  500.  
  501. static const char *
  502.  getrule(const char *strp, register struct rule *rulep)
  503. {
  504.     if (*strp == 'J') {
  505.     /*
  506.        ** Julian day.
  507.      */
  508.     rulep->r_type = JULIAN_DAY;
  509.     ++strp;
  510.     strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
  511.     } else if (*strp == 'M') {
  512.     /*
  513.        ** Month, week, day.
  514.      */
  515.     rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
  516.     ++strp;
  517.     strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
  518.     if (strp == NULL)
  519.         return NULL;
  520.     if (*strp++ != '.')
  521.         return NULL;
  522.     strp = getnum(strp, &rulep->r_week, 1, 5);
  523.     if (strp == NULL)
  524.         return NULL;
  525.     if (*strp++ != '.')
  526.         return NULL;
  527.     strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
  528.     } else if (isdigit(*strp)) {
  529.     /*
  530.        ** Day of year.
  531.      */
  532.     rulep->r_type = DAY_OF_YEAR;
  533.     strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
  534.     } else
  535.     return NULL;        /* invalid format */
  536.     if (strp == NULL)
  537.     return NULL;
  538.     if (*strp == '/') {
  539.     /*
  540.        ** Time specified.
  541.      */
  542.     ++strp;
  543.     strp = getsecs(strp, &rulep->r_time);
  544.     } else
  545.     rulep->r_time = 2 * SECSPERHOUR;    /* default = 2:00:00 */
  546.     return strp;
  547. }
  548.  
  549. /*
  550.    ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
  551.    ** year, a rule, and the offset from GMT at the time that rule takes effect,
  552.    ** calculate the Epoch-relative time that rule takes effect.
  553.  */
  554.  
  555. static time_t
  556.  transtime(time_t janfirst, int year, const struct rule *rulep, long offset)
  557. {
  558.     register int leapyear;
  559.     register time_t value;
  560.     register int i;
  561.     int d, m1, yy0, yy1, yy2, dow;
  562.  
  563.     leapyear = isleap(year);
  564.     switch (rulep->r_type) {
  565.  
  566.     case JULIAN_DAY:
  567.         /*
  568.            ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
  569.            ** years.
  570.            ** In non-leap years, or if the day number is 59 or less, just
  571.            ** add SECSPERDAY times the day number-1 to the time of
  572.            ** January 1, midnight, to get the day.
  573.          */
  574.         value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
  575.         if (leapyear && rulep->r_day >= 60)
  576.         value += SECSPERDAY;
  577.         break;
  578.  
  579.     case DAY_OF_YEAR:
  580.         /*
  581.            ** n - day of year.
  582.            ** Just add SECSPERDAY times the day number to the time of
  583.            ** January 1, midnight, to get the day.
  584.          */
  585.         value = janfirst + rulep->r_day * SECSPERDAY;
  586.         break;
  587.  
  588.     case MONTH_NTH_DAY_OF_WEEK:
  589.         /*
  590.            ** Mm.n.d - nth "dth day" of month m.
  591.          */
  592.         value = janfirst;
  593.         for (i = 0; i < rulep->r_mon - 1; ++i)
  594.         value += mon_lengths[leapyear][i] * SECSPERDAY;
  595.  
  596.         /*
  597.            ** Use Zeller's Congruence to get day-of-week of first day of
  598.            ** month.
  599.          */
  600.         m1 = (rulep->r_mon + 9) % 12 + 1;
  601.         yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
  602.         yy1 = yy0 / 100;
  603.         yy2 = yy0 % 100;
  604.         dow = ((26 * m1 - 2) / 10 +
  605.            1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
  606.         if (dow < 0)
  607.         dow += DAYSPERWEEK;
  608.  
  609.         /*
  610.            ** "dow" is the day-of-week of the first day of the month.  Get
  611.            ** the day-of-month (zero-origin) of the first "dow" day of the
  612.            ** month.
  613.          */
  614.         d = rulep->r_day - dow;
  615.         if (d < 0)
  616.         d += DAYSPERWEEK;
  617.         for (i = 1; i < rulep->r_week; ++i) {
  618.         if (d + DAYSPERWEEK >=
  619.             mon_lengths[leapyear][rulep->r_mon - 1])
  620.             break;
  621.         d += DAYSPERWEEK;
  622.         }
  623.  
  624.         /*
  625.            ** "d" is the day-of-month (zero-origin) of the day we want.
  626.          */
  627.         value += d * SECSPERDAY;
  628.         break;
  629.     }
  630.  
  631.     /*
  632.        ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
  633.        ** question.  To get the Epoch-relative time of the specified local
  634.        ** time on that day, add the transition time and the current offset
  635.        ** from GMT.
  636.      */
  637.     return value + rulep->r_time + offset;
  638. }
  639.  
  640. /*
  641.    ** Given a POSIX section 8-style TZ string, fill in the rule tables as
  642.    ** appropriate.
  643.  */
  644.  
  645. static int tzparse(const char *name, struct state *sp, int lastditch)
  646. {
  647.     const char *stdname;
  648.     const char *dstname;
  649.     int stdlen;
  650.     int dstlen;
  651.     long stdoffset;
  652.     long dstoffset;
  653.     register time_t *atp;
  654.     register unsigned char *typep;
  655.     register char *cp;
  656.     register int load_result;
  657.  
  658.     stdname = name;
  659.     if (lastditch) {
  660.     stdlen = strlen(name);    /* length of standard zone name */
  661.     name += stdlen;
  662.     if (stdlen >= sizeof sp->chars)
  663.         stdlen = (sizeof sp->chars) - 1;
  664.     } else {
  665.     name = getzname(name);
  666.     stdlen = name - stdname;
  667.     if (stdlen < 3)
  668.         return -1;
  669.     }
  670.     if (*name == '\0')
  671.     return -1;
  672.     else {
  673.     name = getoffset(name, &stdoffset);
  674.     if (name == NULL)
  675.         return -1;
  676.     }
  677.     load_result = tzload(TZDEFRULES, sp);
  678.     if (load_result != 0)
  679.     sp->leapcnt = 0;    /* so, we're off a little */
  680.     if (*name != '\0') {
  681.     dstname = name;
  682.     name = getzname(name);
  683.     dstlen = name - dstname;    /* length of DST zone name */
  684.     if (dstlen < 3)
  685.         return -1;
  686.     if (*name != '\0' && *name != ',' && *name != ';') {
  687.         name = getoffset(name, &dstoffset);
  688.         if (name == NULL)
  689.         return -1;
  690.     } else
  691.         dstoffset = stdoffset - SECSPERHOUR;
  692.     if (*name == ',' || *name == ';') {
  693.         struct rule start;
  694.         struct rule end;
  695.         register int year;
  696.         register time_t janfirst;
  697.         time_t starttime;
  698.         time_t endtime;
  699.  
  700.         ++name;
  701.         if ((name = getrule(name, &start)) == NULL)
  702.         return -1;
  703.         if (*name++ != ',')
  704.         return -1;
  705.         if ((name = getrule(name, &end)) == NULL)
  706.         return -1;
  707.         if (*name != '\0')
  708.         return -1;
  709.         sp->typecnt = 2;    /* standard time and DST */
  710.         /*
  711.            ** Two transitions per year, from EPOCH_YEAR to 2037.
  712.          */
  713.         sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
  714.         if (sp->timecnt > TZ_MAX_TIMES)
  715.         return -1;
  716.         sp->ttis[0].tt_gmtoff = -dstoffset;
  717.         sp->ttis[0].tt_isdst = 1;
  718.         sp->ttis[0].tt_abbrind = stdlen + 1;
  719.         sp->ttis[1].tt_gmtoff = -stdoffset;
  720.         sp->ttis[1].tt_isdst = 0;
  721.         sp->ttis[1].tt_abbrind = 0;
  722.         atp = sp->ats;
  723.         typep = sp->types;
  724.         janfirst = 0;
  725.         for (year = EPOCH_YEAR; year <= 2037; ++year) {
  726.         starttime = transtime(janfirst, year, &start,
  727.                       stdoffset);
  728.         endtime = transtime(janfirst, year, &end,
  729.                     dstoffset);
  730.         if (starttime > endtime) {
  731.             *atp++ = endtime;
  732.             *typep++ = 1;    /* DST ends */
  733.             *atp++ = starttime;
  734.             *typep++ = 0;    /* DST begins */
  735.         } else {
  736.             *atp++ = starttime;
  737.             *typep++ = 0;    /* DST begins */
  738.             *atp++ = endtime;
  739.             *typep++ = 1;    /* DST ends */
  740.         }
  741.         janfirst +=
  742.             year_lengths[isleap(year)] * SECSPERDAY;
  743.         }
  744.     } else {
  745.         int sawstd;
  746.         int sawdst;
  747.         long stdfix;
  748.         long dstfix;
  749.         long oldfix;
  750.         int isdst;
  751.         register int i;
  752.  
  753.         if (*name != '\0')
  754.         return -1;
  755.         if (load_result != 0)
  756.         return -1;
  757.         /*
  758.            ** Compute the difference between the real and
  759.            ** prototype standard and summer time offsets
  760.            ** from GMT, and put the real standard and summer
  761.            ** time offsets into the rules in place of the
  762.            ** prototype offsets.
  763.          */
  764.         sawstd = FALSE;
  765.         sawdst = FALSE;
  766.         stdfix = 0;
  767.         dstfix = 0;
  768.         for (i = 0; i < sp->typecnt; ++i) {
  769.         if (sp->ttis[i].tt_isdst) {
  770.             oldfix = dstfix;
  771.             dstfix =
  772.             sp->ttis[i].tt_gmtoff + dstoffset;
  773.             if (sawdst && (oldfix != dstfix))
  774.             return -1;
  775.             sp->ttis[i].tt_gmtoff = -dstoffset;
  776.             sp->ttis[i].tt_abbrind = stdlen + 1;
  777.             sawdst = TRUE;
  778.         } else {
  779.             oldfix = stdfix;
  780.             stdfix =
  781.             sp->ttis[i].tt_gmtoff + stdoffset;
  782.             if (sawstd && (oldfix != stdfix))
  783.             return -1;
  784.             sp->ttis[i].tt_gmtoff = -stdoffset;
  785.             sp->ttis[i].tt_abbrind = 0;
  786.             sawstd = TRUE;
  787.         }
  788.         }
  789.         /*
  790.            ** Make sure we have both standard and summer time.
  791.          */
  792.         if (!sawdst || !sawstd)
  793.         return -1;
  794.         /*
  795.            ** Now correct the transition times by shifting
  796.            ** them by the difference between the real and
  797.            ** prototype offsets.  Note that this difference
  798.            ** can be different in standard and summer time;
  799.            ** the prototype probably has a 1-hour difference
  800.            ** between standard and summer time, but a different
  801.            ** difference can be specified in TZ.
  802.          */
  803.         isdst = FALSE;    /* we start in standard time */
  804.         for (i = 0; i < sp->timecnt; ++i) {
  805.         register const struct ttinfo *ttisp;
  806.  
  807.         /*
  808.            ** If summer time is in effect, and the
  809.            ** transition time was not specified as
  810.            ** standard time, add the summer time
  811.            ** offset to the transition time;
  812.            ** otherwise, add the standard time offset
  813.            ** to the transition time.
  814.          */
  815.         ttisp = &sp->ttis[sp->types[i]];
  816.         sp->ats[i] +=
  817.             (isdst && !ttisp->tt_ttisstd) ?
  818.             dstfix : stdfix;
  819.         isdst = ttisp->tt_isdst;
  820.         }
  821.     }
  822.     } else {
  823.     dstlen = 0;
  824.     sp->typecnt = 1;    /* only standard time */
  825.     sp->timecnt = 0;
  826.     sp->ttis[0].tt_gmtoff = -stdoffset;
  827.     sp->ttis[0].tt_isdst = 0;
  828.     sp->ttis[0].tt_abbrind = 0;
  829.     }
  830.     sp->charcnt = stdlen + 1;
  831.     if (dstlen != 0)
  832.     sp->charcnt += dstlen + 1;
  833.     if (sp->charcnt > sizeof sp->chars)
  834.     return -1;
  835.     cp = sp->chars;
  836.     (void) strncpy(cp, stdname, stdlen);
  837.     cp += stdlen;
  838.     *cp++ = '\0';
  839.     if (dstlen != 0) {
  840.     (void) strncpy(cp, dstname, dstlen);
  841.     *(cp + dstlen) = '\0';
  842.     }
  843.     return 0;
  844. }
  845.  
  846. static void gmtload(struct state *sp)
  847. {
  848.     if (tzload(GMT, sp) != 0)
  849.     (void) tzparse(GMT, sp, TRUE);
  850. }
  851.  
  852. void tzset(void)
  853. {
  854.     register const char *name;
  855.     void tzsetwall(void);
  856.  
  857.     name = getenv("TZ");
  858.     if (name == NULL) {
  859.     tzsetwall();
  860.     return;
  861.     }
  862.     lcl_is_set = TRUE;
  863. #ifdef ALL_STATE
  864.     if (lclptr == NULL) {
  865.     lclptr = (struct state *) malloc(sizeof *lclptr);
  866.     if (lclptr == NULL) {
  867.         settzname();    /* all we can do */
  868.         return;
  869.     }
  870.     }
  871. #endif                /* defined ALL_STATE */
  872.     if (*name == '\0') {
  873.     /*
  874.        ** User wants it fast rather than right.
  875.      */
  876.     lclptr->leapcnt = 0;    /* so, we're off a little */
  877.     lclptr->timecnt = 0;
  878.     lclptr->ttis[0].tt_gmtoff = 0;
  879.     lclptr->ttis[0].tt_abbrind = 0;
  880.     (void) strcpy(lclptr->chars, GMT);
  881.     } else if (tzload(name, lclptr) != 0)
  882.     if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
  883.         (void) gmtload(lclptr);
  884.     settzname();
  885. }
  886.  
  887. void tzsetwall(void)
  888. {
  889.     lcl_is_set = TRUE;
  890. #ifdef ALL_STATE
  891.     if (lclptr == NULL) {
  892.     lclptr = (struct state *) malloc(sizeof *lclptr);
  893.     if (lclptr == NULL) {
  894.         settzname();    /* all we can do */
  895.         return;
  896.     }
  897.     }
  898. #endif                /* defined ALL_STATE */
  899.     if (tzload((char *) NULL, lclptr) != 0)
  900.     gmtload(lclptr);
  901.     settzname();
  902. }
  903.  
  904. /*
  905.    ** The easy way to behave "as if no library function calls" localtime
  906.    ** is to not call it--so we drop its guts into "localsub", which can be
  907.    ** freely called.  (And no, the PANS doesn't require the above behavior--
  908.    ** but it *is* desirable.)
  909.    **
  910.    ** The unused offset argument is for the benefit of mktime variants.
  911.  */
  912.  
  913. /*ARGSUSED */
  914. static void localsub(const time_t * timep, long offset, struct tm *tmp)
  915. {
  916.     register struct state *sp;
  917.     register const struct ttinfo *ttisp;
  918.     register int i;
  919.     const time_t t = *timep;
  920.  
  921.     if (!lcl_is_set)
  922.     tzset();
  923.     sp = lclptr;
  924. #ifdef ALL_STATE
  925.     if (sp == NULL) {
  926.     gmtsub(timep, offset, tmp);
  927.     return;
  928.     }
  929. #endif                /* defined ALL_STATE */
  930.     if (sp->timecnt == 0 || t < sp->ats[0]) {
  931.     i = 0;
  932.     while (sp->ttis[i].tt_isdst)
  933.         if (++i >= sp->typecnt) {
  934.         i = 0;
  935.         break;
  936.         }
  937.     } else {
  938.     for (i = 1; i < sp->timecnt; ++i)
  939.         if (t < sp->ats[i])
  940.         break;
  941.     i = sp->types[i - 1];
  942.     }
  943.     ttisp = &sp->ttis[i];
  944.     /*
  945.        ** To get (wrong) behavior that's compatible with System V Release 2.0
  946.        ** you'd replace the statement below with
  947.        **   t += ttisp->tt_gmtoff;
  948.        **   timesub(&t, 0L, sp, tmp);
  949.      */
  950.     timesub(&t, ttisp->tt_gmtoff, sp, tmp);
  951.     tmp->tm_isdst = ttisp->tt_isdst;
  952.     tzname[tmp->tm_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
  953.     tmp->tm_zone = &sp->chars[ttisp->tt_abbrind];
  954. }
  955.  
  956. struct tm *
  957.  localtime(const time_t * timep)
  958. {
  959.     static struct tm tm;
  960.  
  961.     localsub(timep, 0L, &tm);
  962.     return &tm;
  963. }
  964.  
  965. /*
  966.    ** gmtsub is to gmtime as localsub is to localtime.
  967.  */
  968.  
  969. static void gmtsub(const time_t * timep, long offset, struct tm *tmp)
  970. {
  971.     if (!gmt_is_set) {
  972.     gmt_is_set = TRUE;
  973. #ifdef ALL_STATE
  974.     gmtptr = (struct state *) malloc(sizeof *gmtptr);
  975.     if (gmtptr != NULL)
  976. #endif                /* defined ALL_STATE */
  977.         gmtload(gmtptr);
  978.     }
  979.     timesub(timep, offset, gmtptr, tmp);
  980.     /*
  981.        ** Could get fancy here and deliver something such as
  982.        ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
  983.        ** but this is no time for a treasure hunt.
  984.      */
  985.     if (offset != 0)
  986.     tmp->tm_zone = WILDABBR;
  987.     else {
  988. #ifdef ALL_STATE
  989.     if (gmtptr == NULL)
  990.         tmp->TM_ZONE = GMT;
  991.     else
  992.         tmp->TM_ZONE = gmtptr->chars;
  993. #endif                /* defined ALL_STATE */
  994. #ifndef ALL_STATE
  995.     tmp->tm_zone = gmtptr->chars;
  996. #endif                /* State Farm */
  997.     }
  998. }
  999.  
  1000. struct tm *
  1001.  gmtime(const time_t * timep)
  1002. {
  1003.     static struct tm tm;
  1004.  
  1005.     gmtsub(timep, 0L, &tm);
  1006.     return &tm;
  1007. }
  1008.  
  1009. static void timesub(const time_t * timep, long offset,
  1010.             const struct state *sp, struct tm *tmp)
  1011. {
  1012.     register const struct lsinfo *lp;
  1013.     register long days;
  1014.     register long rem;
  1015.     register int y;
  1016.     register int yleap;
  1017.     register const int *ip;
  1018.     register long corr;
  1019.     register int hit;
  1020.     register int i;
  1021.  
  1022.     corr = 0;
  1023.     hit = FALSE;
  1024. #ifdef ALL_STATE
  1025.     i = (sp == NULL) ? 0 : sp->leapcnt;
  1026. #endif                /* defined ALL_STATE */
  1027. #ifndef ALL_STATE
  1028.     i = sp->leapcnt;
  1029. #endif                /* State Farm */
  1030.     while (--i >= 0) {
  1031.     lp = &sp->lsis[i];
  1032.     if (*timep >= lp->ls_trans) {
  1033.         if (*timep == lp->ls_trans)
  1034.         hit = ((i == 0 && lp->ls_corr > 0) ||
  1035.                lp->ls_corr > sp->lsis[i - 1].ls_corr);
  1036.         corr = lp->ls_corr;
  1037.         break;
  1038.     }
  1039.     }
  1040.     days = *timep / SECSPERDAY;
  1041.     rem = *timep % SECSPERDAY;
  1042. #ifdef mc68k
  1043.     if (*timep == 0x80000000) {
  1044.     /*
  1045.        ** A 3B1 muffs the division on the most negative number.
  1046.      */
  1047.     days = -24855;
  1048.     rem = -11648;
  1049.     }
  1050. #endif                /* mc68k */
  1051.     rem += (offset - corr);
  1052.     while (rem < 0) {
  1053.     rem += SECSPERDAY;
  1054.     --days;
  1055.     }
  1056.     while (rem >= SECSPERDAY) {
  1057.     rem -= SECSPERDAY;
  1058.     ++days;
  1059.     }
  1060.     tmp->tm_hour = (int) (rem / SECSPERHOUR);
  1061.     rem = rem % SECSPERHOUR;
  1062.     tmp->tm_min = (int) (rem / SECSPERMIN);
  1063.     tmp->tm_sec = (int) (rem % SECSPERMIN);
  1064.     if (hit)
  1065.     /*
  1066.        ** A positive leap second requires a special
  1067.        ** representation.  This uses "... ??:59:60".
  1068.      */
  1069.     ++(tmp->tm_sec);
  1070.     tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
  1071.     if (tmp->tm_wday < 0)
  1072.     tmp->tm_wday += DAYSPERWEEK;
  1073.     y = EPOCH_YEAR;
  1074.     if (days >= 0)
  1075.     for (;;) {
  1076.         yleap = isleap(y);
  1077.         if (days < (long) year_lengths[yleap])
  1078.         break;
  1079.         ++y;
  1080.         days = days - (long) year_lengths[yleap];
  1081.     } else
  1082.     do {
  1083.         --y;
  1084.         yleap = isleap(y);
  1085.         days = days + (long) year_lengths[yleap];
  1086.     } while (days < 0);
  1087.     tmp->tm_year = y - TM_YEAR_BASE;
  1088.     tmp->tm_yday = (int) days;
  1089.     ip = mon_lengths[yleap];
  1090.     for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
  1091.     days = days - (long) ip[tmp->tm_mon];
  1092.     tmp->tm_mday = (int) (days + 1);
  1093.     tmp->tm_isdst = 0;
  1094.     tmp->tm_gmtoff = offset;
  1095. }
  1096.  
  1097. /*
  1098.    ** A la X3J11
  1099.  */
  1100.  
  1101. char *
  1102.  asctime(register const struct tm *timeptr)
  1103. {
  1104.     static const char wday_name[DAYSPERWEEK][3] =
  1105.     {
  1106.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  1107.     };
  1108.     static const char mon_name[MONSPERYEAR][3] =
  1109.     {
  1110.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  1111.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  1112.     };
  1113.     static char result[26];
  1114.  
  1115.     (void) sprintf(result, "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n",
  1116.            wday_name[timeptr->tm_wday],
  1117.            mon_name[timeptr->tm_mon],
  1118.            timeptr->tm_mday, timeptr->tm_hour,
  1119.            timeptr->tm_min, timeptr->tm_sec,
  1120.            TM_YEAR_BASE + timeptr->tm_year);
  1121.     return result;
  1122. }
  1123.  
  1124. char *
  1125.  ctime(const time_t * timep)
  1126. {
  1127.     return asctime(localtime(timep));
  1128. }
  1129.  
  1130. /*
  1131.    ** Adapted from code provided by Robert Elz, who writes:
  1132.    **   The "best" way to do mktime I think is based on an idea of Bob
  1133.    **   Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
  1134.    **   It does a binary search of the time_t space.  Since time_t's are
  1135.    **   just 32 bits, its a max of 32 iterations (even at 64 bits it
  1136.    **   would still be very reasonable).
  1137.  */
  1138.  
  1139. #ifndef WRONG
  1140. #define WRONG    (-1)
  1141. #endif                /* !defined WRONG */
  1142.  
  1143. static void normalize(int *tensptr, int *unitsptr, int base)
  1144. {
  1145.     if (*unitsptr >= base) {
  1146.     *tensptr += *unitsptr / base;
  1147.     *unitsptr %= base;
  1148.     } else if (*unitsptr < 0) {
  1149.     --*tensptr;
  1150.     *unitsptr += base;
  1151.     if (*unitsptr < 0) {
  1152.         *tensptr -= 1 + (-*unitsptr) / base;
  1153.         *unitsptr = base - (-*unitsptr) % base;
  1154.     }
  1155.     }
  1156. }
  1157.  
  1158. static int tmcomp(register const struct tm *atmp, register const struct tm *btmp)
  1159. {
  1160.     register int result;
  1161.  
  1162.     if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
  1163.     (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
  1164.     (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
  1165.     (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
  1166.     (result = (atmp->tm_min - btmp->tm_min)) == 0)
  1167.     result = atmp->tm_sec - btmp->tm_sec;
  1168.     return result;
  1169. }
  1170.  
  1171. static time_t
  1172.  time2(struct tm *tmp,
  1173.        void (*funcp) (const time_t *, long, struct tm *),
  1174.        long offset,
  1175.        int *okayp)
  1176. {
  1177.     register const struct state *sp;
  1178.     register int dir;
  1179.     register int bits;
  1180.     register int i, j;
  1181.     register int saved_seconds;
  1182.     time_t newt;
  1183.     time_t t;
  1184.     struct tm yourtm, mytm;
  1185.  
  1186.     *okayp = FALSE;
  1187.     yourtm = *tmp;
  1188.     if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
  1189.     normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
  1190.     normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
  1191.     normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
  1192.     normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
  1193.     while (yourtm.tm_mday <= 0) {
  1194.     --yourtm.tm_year;
  1195.     yourtm.tm_mday +=
  1196.         year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
  1197.     }
  1198.     for (;;) {
  1199.     i = mon_lengths[isleap(yourtm.tm_year +
  1200.                    TM_YEAR_BASE)][yourtm.tm_mon];
  1201.     if (yourtm.tm_mday <= i)
  1202.         break;
  1203.     yourtm.tm_mday -= i;
  1204.     if (++yourtm.tm_mon >= MONSPERYEAR) {
  1205.         yourtm.tm_mon = 0;
  1206.         ++yourtm.tm_year;
  1207.     }
  1208.     }
  1209.     saved_seconds = yourtm.tm_sec;
  1210.     yourtm.tm_sec = 0;
  1211.     /*
  1212.        ** Calculate the number of magnitude bits in a time_t
  1213.        ** (this works regardless of whether time_t is
  1214.        ** signed or unsigned, though lint complains if unsigned).
  1215.      */
  1216.     for (bits = 0, t = 1; t > 0; ++bits, t <<= 1);
  1217.     /*
  1218.        ** If time_t is signed, then 0 is the median value,
  1219.        ** if time_t is unsigned, then 1 << bits is median.
  1220.      */
  1221.     t = (t < 0) ? 0 : ((time_t) 1 << bits);
  1222.     for (;;) {
  1223.     (*funcp) (&t, offset, &mytm);
  1224.     dir = tmcomp(&mytm, &yourtm);
  1225.     if (dir != 0) {
  1226.         if (bits-- < 0)
  1227.         return WRONG;
  1228.         if (bits < 0)
  1229.         --t;
  1230.         else if (dir > 0)
  1231.         t -= (time_t) 1 << bits;
  1232.         else
  1233.         t += (time_t) 1 << bits;
  1234.         continue;
  1235.     }
  1236.     if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
  1237.         break;
  1238.     /*
  1239.        ** Right time, wrong type.
  1240.        ** Hunt for right time, right type.
  1241.        ** It's okay to guess wrong since the guess
  1242.        ** gets checked.
  1243.      */
  1244.     sp = (const struct state *)
  1245.         ((funcp == localsub) ? lclptr : gmtptr);
  1246. #ifdef ALL_STATE
  1247.     if (sp == NULL)
  1248.         return WRONG;
  1249. #endif                /* defined ALL_STATE */
  1250.     for (i = 0; i < sp->typecnt; ++i) {
  1251.         if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
  1252.         continue;
  1253.         for (j = 0; j < sp->typecnt; ++j) {
  1254.         if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
  1255.             continue;
  1256.         newt = t + sp->ttis[j].tt_gmtoff -
  1257.             sp->ttis[i].tt_gmtoff;
  1258.         (*funcp) (&newt, offset, &mytm);
  1259.         if (tmcomp(&mytm, &yourtm) != 0)
  1260.             continue;
  1261.         if (mytm.tm_isdst != yourtm.tm_isdst)
  1262.             continue;
  1263.         /*
  1264.            ** We have a match.
  1265.          */
  1266.         t = newt;
  1267.         goto label;
  1268.         }
  1269.     }
  1270.     return WRONG;
  1271.     }
  1272.   label:
  1273.     t += saved_seconds;
  1274.     (*funcp) (&t, offset, tmp);
  1275.     *okayp = TRUE;
  1276.     return t;
  1277. }
  1278.  
  1279. static time_t
  1280.  time1(struct tm *tmp,
  1281.        void (*funcp) (const time_t *, long, struct tm *),
  1282.        long offset)
  1283. {
  1284.     register time_t t;
  1285.     register const struct state *sp;
  1286.     register int samei, otheri;
  1287.     int okay;
  1288.  
  1289.     if (tmp->tm_isdst > 1)
  1290.     tmp->tm_isdst = 1;
  1291.     t = time2(tmp, funcp, offset, &okay);
  1292.     if (okay || tmp->tm_isdst < 0)
  1293.     return t;
  1294.     /*
  1295.        ** We're supposed to assume that somebody took a time of one type
  1296.        ** and did some math on it that yielded a "struct tm" that's bad.
  1297.        ** We try to divine the type they started from and adjust to the
  1298.        ** type they need.
  1299.      */
  1300.     sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
  1301. #ifdef ALL_STATE
  1302.     if (sp == NULL)
  1303.     return WRONG;
  1304. #endif                /* defined ALL_STATE */
  1305.     for (samei = 0; samei < sp->typecnt; ++samei) {
  1306.     if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
  1307.         continue;
  1308.     for (otheri = 0; otheri < sp->typecnt; ++otheri) {
  1309.         if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
  1310.         continue;
  1311.         tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
  1312.         sp->ttis[samei].tt_gmtoff;
  1313.         tmp->tm_isdst = !tmp->tm_isdst;
  1314.         t = time2(tmp, funcp, offset, &okay);
  1315.         if (okay)
  1316.         return t;
  1317.         tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
  1318.         sp->ttis[samei].tt_gmtoff;
  1319.         tmp->tm_isdst = !tmp->tm_isdst;
  1320.     }
  1321.     }
  1322.     return WRONG;
  1323. }
  1324.  
  1325. time_t
  1326. mktime(struct tm * tmp)
  1327. {
  1328.     return time1(tmp, localsub, 0L);
  1329. }
  1330.  
  1331. /* Call tzset() if necessary */
  1332. void _tzset(void)
  1333. {
  1334.     if (!lcl_is_set)
  1335.     tzset();
  1336. }
  1337.